import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './assets/index.css'
import './assets/common.css'
import axios from 'axios'
import moment from 'moment'
import Vuex from 'vuex'
import './assets/mui/csss/icons-extra.css'
import './assets/mui/csss/mui.min.css'

// vant ui
import { Lazyload, Pagination, Sku } from 'vant'
// 这是头部
import { Header, Tabbar, TabItem, Swipe, SwipeItem, Button, Switch } from 'mint-ui'
Vue.component(Header.name, Header)
Vue.component(Tabbar.name, Tabbar)
Vue.component(TabItem.name, TabItem)
Vue.component(Swipe.name, Swipe)
Vue.component(SwipeItem.name, SwipeItem)
Vue.component(Button.name, Button)
Vue.component(Switch.name, Switch)
Vue.config.productionTip = false

Vue.use(Vuex)
Vue.use(Lazyload)
Vue.use(Pagination)
Vue.use(Sku)
    // 对时间的格式化
Vue.filter('dataFormat', function(datastr, pattern = 'YYYY-MM-DD hh:mm:ss') {
        return moment(datastr).format(pattern)
    })
    // 设置默认根路由
axios.defaults.baseURL = 'http://timemeetyou.com:8889/api/private/v1/'
Vue.prototype.$http = axios
    // 设置路由拦截器
    // axios 拦截器,拦截请求
axios.interceptors.request.use(config => {
        config.headers.Authorization = window.sessionStorage.getItem('token')
        return config
    })
    // axios 拦截响应
axios.interceptors.response.use(config => {
    return config
})

var shoplist = JSON.parse(localStorage.getItem('list') || '[]')
const store = new Vuex.Store({
    state: {
        id: '',
        goodsid: '',
        shoplist: shoplist,
        acount: 0
    },
    mutations: {
        increment(state, goodsid) {
            state.id = goodsid
                // console.log(state.id)
        },
        incrementgoods(state, goodsid) {
            state.goodsid = goodsid
        },
        incrementshop(state, shop) {
            // 先判断购物车里面有没有已经添加的
            var flag = false
            state.shoplist.forEach(item => {
                if (item.goodsId === shop.goodsId) {
                    item.selectedNum += parseInt(shop.selectedNum)
                    flag = true
                    return true
                }
            })
            if (!flag) {
                state.shoplist.push(shop)
            }
            localStorage.setItem('list', JSON.stringify(state.shoplist))
        },
        // 用来计算已经选中的数量和价格
        incrementamount(state, shopid) {
            if (shopid.value) {
                state.acount += parseInt(shopid.selectedNum)
            } else {
                state.acount -= parseInt(shopid.selectedNum)
            }
        }
    },
    getters: {
        // 这是返回加入购物车的列表
        // optCount: function(state) {
        //     return state.shoplist
        // },
        // 这是返回计算已勾选商品件数和价格
        optAmount: function(state) {
            return state.acount
        }
    }
})

new Vue({
    router,
    store: store,
    render: h => h(App)
}).$mount('#app')